package com.sduept.nwld.dataserver.manager.statistics;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCounterMeasure;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCounterMeasureSub;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CounterMeasuresManager {

	@Autowired
	private SuperDAO dao;
	
	public List<ProtectCounterMeasure> findAllProtectCounterMeasure() {
		return dao.findAll(ProtectCounterMeasure.class);
	}
	
	public List<ProtectCounterMeasureSub> findAllProtectCounterMeasureSub() {
		return dao.findAll(ProtectCounterMeasureSub.class);
	}
	
	public List<String> getAllProtectCounterMeasureNames() {
		return (List<String>) dao.nativeQuery("select distinct name from t_count_protect_counter_measure");
	}
	
	public List<String> getSubcompany() {
		return (List<String>) dao.nativeQuery("select distinct sub_company from t_count_protect_counter_measure_sub_statistics");
	}
	
	public List<ProtectCounterMeasure> getProtectCounterMeasureByName(String name) {
		return dao.findByCondition(ProtectCounterMeasure.class, "name = ?", name);
	}
	
	
	public List<ProtectCounterMeasure> getProtectCounterMeasureById(String id) {
		return dao.findByCondition(ProtectCounterMeasure.class, "id = ?", id);
	}
	
	public ProtectCounterMeasure create(ProtectCounterMeasure protectCounterMeasure) {
		return dao.createOrUpdate(protectCounterMeasure, ProtectCounterMeasure.class);
		
	}
	
	public ProtectCounterMeasureSub create(ProtectCounterMeasureSub protectCounterMeasureSub) {
		return dao.createOrUpdate(protectCounterMeasureSub, ProtectCounterMeasureSub.class);
		
	}
	
	public void deleteProtectCounterMeasureById(String id) {
		dao.delete(id, ProtectCounterMeasure.class);
	}
	
	public void deleteProtectCounterMeasureSubById(String id) {
		dao.delete(id, ProtectCounterMeasureSub.class);
	}
	
	public List<ProtectCounterMeasureSub> getProtectCounterMeasureSubByCondition(int year,String pid,String subCompany) {
		return dao.findByCondition(ProtectCounterMeasureSub.class, "pid=? and year=? and company=?", pid,year,subCompany);
	}
	
	public List<ProtectCounterMeasureSub> query(String pid,String company) {
		Object[]  param = getWhereCase(pid, company);
		String whereClause = (String) param[0];
		Object[] whereArgs = (Object[])param[1];
		List<ProtectCounterMeasureSub> pcms = new ArrayList<ProtectCounterMeasureSub>();
		if (StringUtils.isEmpty(whereClause)) {
			pcms = dao.findAll(ProtectCounterMeasureSub.class);
		} else {
			pcms = dao.findByCondition(whereClause, whereArgs, ProtectCounterMeasureSub.class);
		}
		return pcms;
	}
	
	public Object[] getWhereCase(String pid,String company) {
		String whereClause = "";
		List<Object> paramValues = new ArrayList<>();
		if (!StringUtils.isEmpty(pid)) {
			whereClause += " and pid = ?  ";
			paramValues.add(pid);
		}
		if (!StringUtils.isEmpty(company)) {
			whereClause += "and company = ?  ";
			paramValues.add(company);
		}
		whereClause = whereClause.replaceFirst("and", "");
		Object[] rtn = new Object[]{whereClause,paramValues.toArray(new Object[0])};
		return rtn;
	}
}
